<?php

namespace Common\Model;

/**
 * 订单退款记录信息表
 * @modify trainyao 2016-05-17 创建
 */
Class OrderRefundModel extends \Common\Model\BaseModel {

    protected $trueTableName = 'tb_order_refund';
    protected $field = 'id,hotelentity_id,orderroom_id,type,tradenumber,batchnumber,price,refundprice,reason,apply,status,createtime,updatetime,operator,isdelete';

    protected function _before_insert(&$data, $options) {
        $data['operator'] = 0;
    }

    /**
     * 添加退款记录
     * @modify trainyao 2016-05-17   创建
     * @modify trainyao 2016-06-20   增加退款类型
     * @param  integer   $hotelId     客栈ID
     * @param  integer   $orderRoomId 子订单ID
     * @param  string   $price     订单退款金额
     * @param  string   $reason      申请退款理由
     * @param  integer   $type      退款类型
     */
    public function addInfo($hotelId, $orderRoomId, $price, $reason = null, $type = null) {
        $data = array(
            'hotelentity_id' => $hotelId,
            'orderroom_id' => $orderRoomId,
            'price' => $price,
        );
        if (!is_null($reason)) {
            $data['reason'] = $reason;
        }
        if (!is_null($type)) {
            $data['type'] = $type;
        }

        return empty($this->add($data)) ? false : true;
    }

    /**
     * 根据ID获取信息
     * @modify trainyao 2016-06-22 创建
     * @param  integer   $id        条目ID
     * @return array              信息数组
     */
    public function getInfo($id) {
        $option = array(
            'id' => $id,
            'isdelete' => NOT_DELETE,
        );

        $data = $this->where($option)->field($this->field)->find();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据子订单ID获取数据
     * @modify trainyao 2016-05-17   创建
     * @modify trainyao 2016-05-19   增加多个子订单查询
     * @param  intege   $orderRoomId 子订单ID
     * @return array                信息数组
     */
    public function getInfoByOrder($orderRoomId) {
        $option = array(
            'isdelete' => NOT_DELETE,
        );
        if (is_array($orderRoomId)) {
            $orderRoomId = implode(',', $orderRoomId);
        }
        if (strpos($orderRoomId, ',') === false) {
            $option['orderroom_id'] = $orderRoomId;
        } else {
            $option['orderroom_id'] = array('in', $orderRoomId);
        }

        $data = $this->where($option)->field($this->field)->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据客栈ID获取信息
     * @modify trainyao 2016-05-17 创建
     * @param  integer   $hotelId   客栈ID
     * @return array              信息数组
     */
    public function getInfoByHotel($hotelId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );
        $data = $this->where($option)->field($this->field)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 保存信息
     * @modify trainyao 2016-05-17   创建
     * @param  integer   $id          条目ID
     * @param  integer   $status      退款操作状态
     * @param  string   $refundPrice 已退款金额
     * @param  string   $apply       驳回理由
     * @return boolean                操作结果
     */
    public function saveInfo($id, $status = null, $refundPrice = null, $apply = null) {
        $option = array(
            'id' => $id,
            'isdelete' => NOT_DELETE,
        );
        $data = array();
        if (!is_null($status)) {
            $data['status'] = $status;
        }
        if (!is_null($refundPrice)) {
            $data['refundprice'] = $refundPrice;
        }
        if (!is_null($apply)) {
            $data['apply'] = $apply;
        }

        return $this->where($option)->save($data) === false ? false : true;
    }
}
